function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return "";
}

function goIndex() {
    location.href = 'index.html';
}

var appid = '7';
var isApp = false;
var appName = "";

Zepto(function ($) {
    $('#captcha').attr('src',Global.api_url+'/server/captcha/simple-php-captcha.php?_CAPTCHA&');
    appid = getQueryString('appid');
    
    if(appid == ''){
        $().toastmessage('showErrorToast', '没有传入应用id');
        return false;
    }
    localStorage.setItem('caid',appid);
    $.getJSON(Global.api_url + "/api/cloud/index.php?version=1&module=app_all_not_limit_find", function (data) {
        $.each(data.Variables.data, function(index , obj){
            if(obj.caid === appid){
                var img = $("#logo img");
                appName = obj.name;
                img.attr("alt" , appName);
                img.attr("title" , appName);
                img.attr("src" , "login/images/"+ appid +".png");
                isApp = true;
            }
        });
    });

    //0.根据是否记住清理登陆信息或者跳转
    var _remember = localStorage.getItem('remember'); //应用ID
    if (_remember) {
        //console.log(_remember);
        var _managerId = localStorage.getItem('managerId');
        var _role = localStorage.getItem('role');
        var _managerName = localStorage.getItem('managerName');
        if (_managerId && _role && _managerName) {
            //goIndex();
        }
    }


$('#captcha').click(function(){
    $(this).attr('src',Global.api_url+'/server/captcha/simple-php-captcha.php?_CAPTCHA&');
});

    //1.验证
    //2.报错显示错误信息
    //3.提交并显示loading
    //4.成功记录内容，并跳转 
    $("#appLoadingIndicator").remove();
    $("#submit").on('click', function () {

        var username = $('#username').val();
        var password = $('#password').val();
        var code = $('#code').val();
        var remember = $('#remember').attr('checked'); 

        if (appid == "" || !isApp) {
            $().toastmessage('showErrorToast', '没有此应用。');
            return false;
        }
        if (!happy.minLength(username, 3) || !happy.maxLength(username, 20)) {
            $().toastmessage('showErrorToast', '用户名必须是3-20位');
            return false;
        }
        if (!happy.minLength(password, 3) || !happy.maxLength(password, 16)) {
            $().toastmessage('showErrorToast', '密码必须是3-16位');
            return false;
        }
        if (happy.empty(code)) {
            $().toastmessage('showErrorToast', '验证码不能为空');
            return false;
        } else if (!happy.maxLength(code, 4)) {
            $().toastmessage('showErrorToast', '验证码不能超过4位');
            return false;
        }
        localStorage.setItem('cadid', 6);
        $.ajax({type: 'GET',
            url: Global.api_url + '/api/cloud/index.php?version=1&module=manager_login',
            data: {'username': username, 'password': password, 'code': code,'caid': appid},
            success: function (data) { 
                if (data.Variables.Result.code == '0') { 
                    localStorage.setItem('managerId', data.Variables.member_uid);
                    localStorage.setItem('managerName', data.Variables.member_username);
                    localStorage.setItem('role', 'role');
                    localStorage.setItem('remember', remember);
                    localStorage.setItem('uid', data.Variables.member_uid);
                    localStorage.setItem('appname', data.Variables.app_name);
                    localStorage.setItem('username', username);

                    localStorage.setItem('appName', appName);
                    goIndex();
                } else {
                    // console.log(data.Variables.Message.messagestr);
                    $().toastmessage('showNoticeToast', data.Message.messagestr);
                }
            },
            dataType: 'jsonp'});
        return false;
    });
})